import { observable, action,toJS } from 'mobx';
import {WeaLocaleProvider,WeaError} from "ecCom"
import { Modal, message } from "antd"
import * as  Apis from '../apis/project';
const getLabel = WeaLocaleProvider.getLabel;

class PrjImportStore {
    title = getLabel(81757,"项目导入");
    topTab = [ 
        {key: 'prjimport',title: getLabel(81757,"项目导入")},
        {key: 'taskimport', title: getLabel(383858,"任务导入")},
    ];
    @observable selectTabkey = "prjimport";
	@observable	isuse = 0;
    @observable	replaceDatas = [];
    @observable	mandstr = "";
    @observable	otherstr = "";
    @observable	taskmandstr = "";
    @observable	taskotherstr = "";
    @observable loading = false;
    @observable isrefresh = new Date().getTime();
    @observable taskisrefresh = new Date().getTime();
    @observable hasPrjUpload = false;
    @observable hasTaskUpload = false;
    @observable taskreplaceDatas = [];   //项目导入默认值
    @observable prjType = "";   //项目导入默认值
    @observable taskType = "";   //项目导入默认值
    @observable spinning =false;
    @observable prjmsg = "";
    @observable taskmsg = "";
    @observable imp_totalCount = "";//总数据数
    @observable imp_successCount = "";//正确导入数
    @observable imp_loseCount = "";//失败条数
    @observable reflesh = 0;
    @observable filelist = [];
    @observable excelfile = '';
    @observable taskfilelist = [];
    @observable taskexcelfile = '';
    @observable prjremind = '';
    @observable taskremind = '';
    @observable selectremind = '';
    //wbs导入
    @observable filetype = '1';//任务导入类型
    @observable taskwbsfieldstr = "";
    @observable visible = false;
    @observable columns = []
    @observable columnDatas = []
    @observable selectRowKeys = [];
    @observable saveVisible = false;
    @observable readonly = false;
    
    @observable taskDataParams = {};

    @action 
    initDatas=(params={})=>{
        this.loading = true;
        let _this = this;
        Apis.prjimptopt(params).then(data=>{
            _this.isuse = data.isuse;
            _this.mandstr = data.mandstr;
            _this.otherstr = data.otherstr;
            Apis.doPrjTemplateFile({isuse:this.isuse,temptype:'prjtask',isdata:params.isdata}).then(data=>{
                _this.loading = false;
           });;
       })
    }

    @action 
    initTaskDatas=(params={})=>{
        this.loading = true;
        let _this = this;
        Apis.prjimptopt(params).then(data=>{
            _this.taskmandstr = data.mandstr;
            _this.taskotherstr = data.otherstr;
            _this.taskwbsfieldstr = data.taskwbsfieldstr;
            _this.taskDataParams = data.taskDataParams;
            Apis.doPrjTemplateFile({temptype:'prjtask',isdata:params.isdata}).then(data=>{
                _this.loading = false;
           });
       })
    }

    @action 
    savePrjType=(ids,data,type)=>{
        if(type == "prj"){
            this.prjType = ids;
            this.replaceDatas = data;
        }else{
            this.taskType = ids;
            this.taskreplaceDatas = data;
        } 
    }

    @action 
    saveFileType=(value)=>{
        this.filetype = value;
        this.taskmsg = "";
        this.taskexcelfile="";
        this.taskfilelist=[];
    }
    @action 
    saveDefaultInfo=(prjid,prjname)=>{
        this.selectTabkey = 'taskimport';
        this.readonly = true;
        this.taskType = prjid;
        this.taskreplaceDatas = [{id:prjid,name:prjname}];       
    }

    @action
    regenTemplate=()=>{
        this.isrefresh = new Date().getTime();
        this.spinning = true;
        let params = {isuse:this.isuse,temptype:'prjtype',isdata:'1',prjtype:this.prjType};
        Apis.doPrjTemplateFile(params).then(data=>{
            this.spinning = false;
            Modal.success({
                title: getLabel(128231,"模板创建完成，可以下载了。"),
                content: '',
            });
        })
    }

    //wbs字段设置
    @action
    setPrjWbsSetField = (data) => {
        this.columns = data.columns;
        this.columnDatas = data.columnDatas;
    }

    @action
    handleDialog = (val) => {
        this.visible = val;
        if(val){
            Apis.getTaskWbsField().then(data=>{
                this.setPrjWbsSetField(data);
            })
        }else{
            this.columns = []
            this.columnDatas = []
            this.selectRowKeys = [];
        }   
    }

    @action
    setSelectRowKeys = (keys) => {
        this.selectRowKeys = keys;
    }
    

    @action
    setColumnDatas = (datas) => {
        let _this = this;
        _this.columnDatas = datas;
    }

    @action
    saveTaskWbsField = () => {
        let params = {fields: JSON.stringify(this.columnDatas)};
        let _this = this;
        if(this.check_field()){
            _this.saveVisible = true;
            Apis.doTaskWbsFieldSave(params).then(data=>{
                if (data.success) {
                    _this.taskwbsfieldstr = data.taskwbsfieldstr;
                    message.success(getLabel(18758, "保存成功"));
                    Apis.getTaskWbsField().then(data=>{
                        _this.setPrjWbsSetField(data);
                        _this.saveVisible = false;
                    })
                } else {
                    message.error(data.errmsg);
                }
            })
        }
        
    }

    check_field = () => {
        let fields = toJS(this.columnDatas);
        let bool = true;
        fields.forEach(item => {
            if (item.com.wbsfield[0].viewAttr == 3) {
                if (item.wbsfield.replace(" ", "") == "") {
                    bool = false;
                }
            }
        })
        return bool;
    }

    doImport=(refTaskList)=>{
        if(this.selectTabkey == "prjimport"){  //项目导入
            if(this.excelfile==''){
                this.prjremind.showError();
                return;
            }else{
                let _this = this;
                let params = {isdata:"1",isuse:this.isuse,prjtype:this.prjType,prjexcelfile:this.excelfile||''};
                this.spinning = true;
                Apis.doPrjimpopt(params).then(data => {
                    if(data) {					
                        _this.reflesh++;								
                        _this.spinning = false;  
                        _this.prjmsg = data.msg;
                        _this.imp_totalCount = data.totalCount;
                        _this.imp_successCount = data.successCount;
                    }
                });
            } 
        }else{//任务导入
            if(this.taskexcelfile==''){
                this.taskremind.showError();
                return;
            }else{
                if(!this.taskType||this.taskType==""){
                    this.selectremind.showError();
                    return ;
                }else{
                    let _this = this;
                    let _filetype = this.filetype;
                    let params = {isdata:"2",filetype:this.filetype,parentprj:this.taskType,taskexcelfile:this.taskexcelfile||''};
                    this.spinning = true;
                    Apis.doPrjimpopt(params).then(data => {
                        if(data) {					
                            _this.reflesh++;						
                            _this.spinning = false;
                            _this.taskmsg = data.msg;
                            _this.filetype = _filetype;
                            _this.imp_totalCount = data.totalCount;
                            _this.imp_successCount = data.successCount;
                        }
                        data.successCount>0 && refTaskList && refTaskList();
                          
                    })
                }
            }
        }
    }

    changeTab=(key)=>{
        this.selectTabkey = key;
        if('prjimport'==key){
            this.title = getLabel(81757,"项目导入");
        }else if('taskimport'==key){
            this.title = getLabel(383858,"任务导入");
        }
    }

    clearStatus=()=>{
        this.replaceDatas = [];
        this.prjmsg = "";
        this.taskmsg = "";
        this.imp_totalCount = "";
        this.imp_successCount = "";
        this.selectTabkey = "prjimport";
        this.isuse = 0;
        this.taskreplaceDatas = [];
        this.prjType = ""; 
        this.taskType = ""; 
        this.reflesh++;
        this.excelfile="";
        this.filelist=[];
        this.taskexcelfile="";
        this.taskfilelist=[];
        this.columns = []
        this.columnDatas = []
        this.selectRowKeys = [];
        this.readonly = false;
    }
}

const prjImportStore = new PrjImportStore()
export default prjImportStore;